package com.example.voicebox;

import android.os.Bundle;
import android.speech.tts.Voice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.voicebox.databinding.FragmentVoiceListBinding;
import com.example.voicebox.databinding.ItemVoiceBoxBinding;
import com.example.voicebox.model.VoiceBox;
import com.example.voicebox.tools.VoicePlayer;
import com.example.voicebox.vm.VoiceBoxVM;
import com.example.voicebox.vm.VoiceListVM;

import java.util.List;


/**
 *音效列表的控制代码
 * */
public class VoiceListFragment extends Fragment {

    private static final  String TAG = "VoiceListFragment";
    private VoicePlayer mVoicePlayer;
    private RecyclerView mVoiceListRecyclerView;//RecyclerView在其他方法中也会用到，因此需要把它变成一个成员变量


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mVoicePlayer = new VoicePlayer(getActivity());
    }

    @Nullable
    @Override
    //加载布局文件
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        FragmentVoiceListBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_voice_list, container, false);
        //把viewmodel对象传入到对应的布局xml中
        binding.setVoiceListVM(new VoiceListVM(mVoicePlayer));
        mVoiceListRecyclerView = binding.voiceListRecyclerview;
        mVoiceListRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
        mVoiceListRecyclerView.setAdapter(new VoiceListAdapter(mVoicePlayer.getVoiceBoxList()));


//        //通过布局封装器inflater拿到布局文件（R.layout.fragment_voice_list）
//        //将布局fragment_voice_list解析成一个对象名叫view
//        View view = inflater.inflate(R.layout.fragment_voice_list, container, false);//Alt+Enter快速返回返回值
//
//
//        loadBGMButton(view);
//        //拿到view对象之后在通过findbyid拿到recycleview
//        mVoiceListRecycleView = view.findViewById(R.id.voice_list_recyclerview);
//        //列表展示,按钮展示每排7个
//        mVoiceListRecycleView.setLayoutManager(new GridLayoutManager(getActivity(),7));
//        //装载数据，传方法，将音频资源拿到适配器中进行数据的加载
//        mVoiceListRecycleView.setAdapter(new VoiceListAdapter(mVoicePlayer.getVoiceBoxList()));
        return binding.getRoot();

    }



    //适配器
    private class VoiceListAdapter extends RecyclerView.Adapter<VoiceViewHolder>{

        /**
         * 列表项的元素
         * */
        private List<VoiceBox> mVoiceBoxList;


        public VoiceListAdapter(List<VoiceBox> voiceBoxList){
            mVoiceBoxList = voiceBoxList;
        }

        @NonNull
        @Override
        public VoiceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            //拿到item_voice_box.xml的数据绑定类
            ItemVoiceBoxBinding binding = DataBindingUtil.inflate(inflater, R.layout.item_voice_box, parent, false);
            //把绑定类传到列表单元项的控制类里
            VoiceViewHolder viewHolder = new VoiceViewHolder(binding);
//            View itemView = inflater.inflate(R.layout.item_voice_box, parent, false);
//            VoiceViewHolder viewHolder = new VoiceViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull VoiceViewHolder holder, int position) {
            VoiceBox voiceBox = mVoiceBoxList.get(position);
            holder.bind(voiceBox);
        }

        @Override
        public int getItemCount() {
            return mVoiceBoxList.size();
        }
    }
    //具体去管理列表单元的管理
    private class VoiceViewHolder extends RecyclerView.ViewHolder{

        private ItemVoiceBoxBinding mBinding;

        public VoiceViewHolder(@NonNull ItemVoiceBoxBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
            //给绑定类的viewModel赋值
            VoiceBoxVM voiceBoxVM = new VoiceBoxVM(mVoicePlayer);
            mBinding.setVoiceBoxVM(voiceBoxVM);
        }

        public void bind(VoiceBox voiceBox) {
            //把之前装载的viewmodel对象拿出来
            VoiceBoxVM voiceBoxVM = mBinding.getVoiceBoxVM();
            //调用对象里的setVoiceBox方法，传入具体某个声音对象
            voiceBoxVM.setVoiceBox(voiceBox);
        }
    }

    @Override
    public void onStop(){
        super.onStop();
        mVoicePlayer.pauseBGM();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        mVoicePlayer.stopSonic();
        mVoicePlayer.stopBGM();
    }







}
